import React from 'react';
import Toast from 'react-native-easy-toast';

export class ToastManager {
  /**
   * 静态toast
   */
  static toast;

  /**
   * 显示toast
   * showToast
   * @param text
   * @param duration
   * @param callback
   */
  static show(text, duration = 3000, callback) {
    if (this.toast && text && typeof text === 'string') {
      this.toast.show(text, duration, callback);
    }
  }

  /**
   * 关闭toast
   * closeToast
   * @param duration
   */
  static close(duration) {
    if (this.toast) {
      this.toast.close(duration);
    }
  }
}

export default class ToastComponent extends React.Component {
  /**
   * 组件被移除的时候
   */
  componentWillUnmount() {
    ToastManager.toast = null;
  }

  render() {
    return <Toast ref={e => (ToastManager.toast = e)} position="center" />;
  }
}
